package org.opencare.plugins.bootstrap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.opencare.plugins.listener.Listener;
import org.tanukisoftware.wrapper.WrapperListener;
import org.tanukisoftware.wrapper.WrapperManager;

public class BootListener implements WrapperListener {
	/** Logger for this class and subclasses */
	protected final Log logger = LogFactory.getLog(getClass());
	private Listener m_listener;

	public BootListener() {
		// TODO Auto-generated constructor stub
	}

	public void controlEvent(int event) {
		// TODO Auto-generated method stub
		if (WrapperManager.isControlledByNativeWrapper()) {
			// The Wrapper will take care of this event
		} else {
			// We are not being controlled by the Wrapper, so
			// handle the event ourselves.
			if ((event == WrapperManager.WRAPPER_CTRL_C_EVENT)
					|| (event == WrapperManager.WRAPPER_CTRL_CLOSE_EVENT)
					|| (event == WrapperManager.WRAPPER_CTRL_SHUTDOWN_EVENT)) {
				WrapperManager.stop(0);
			}
		}
	}

	public Integer start(String[] args) {
		// TODO Auto-generated method stub
		m_listener = new Listener();
		m_listener.start();
		return null;
	}

	public int stop(int exitCode) {
		// TODO Auto-generated method stub
		m_listener.stop();
		return exitCode;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		WrapperManager.start(new BootListener(), args);
	}

}
